using System.Web.Mvc;
using Headspring.Training.Core.Model;
using Headspring.Training.Core.Services;
using MvcContrib;
using StructureMap;

namespace Headspring.Training.UI.Filters
{
	public class AuthenticationFilterAttribute : ActionFilterAttribute
	{
		private readonly IUserSession _session;

		public AuthenticationFilterAttribute(IUserSession session)
		{
			_session = session;
		}

		public AuthenticationFilterAttribute()
			: this(ObjectFactory.GetInstance<IUserSession>())
		{
		}

		public override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			var controller = (Controller) filterContext.Controller;
			Employee user = _session.GetCurrentUser();
			controller.ViewData.Add(user);
		}
	}
}